import { ProjectName } from '@/config'
import { uniqBy } from 'lodash'
import { ElNotification } from 'element-plus'

export const isDev = import.meta.env.MODE === 'development'

export function setPageTitle(titleText: string | unknown) {
  window.document.title = `${titleText ? ` ${titleText} - ` : ''}${ProjectName}`
}

/**
 * @description 获取localStorage
 * @param {String} key Storage名称
 * @return string
 */
export function localGet(key: string) {
  const value = window.localStorage.getItem(key)
  if (!value) {
    return ''
  }

  return JSON.parse(window.localStorage.getItem(key) as string)
}

/**
 * @description 存储localStorage
 * @param {String} key Storage名称
 * @param {Any} value Storage值
 * @return void
 */
export function localSet(key: string, value: any) {
  if (!key) return ''
  window.localStorage.setItem(key, JSON.stringify(value))
}

/**
 * @description 清除localStorage
 * @param {String} key Storage名称
 * @return void
 */
export function localRemove(key: string) {
  if (!key) return ''
  window.localStorage.removeItem(key)
}

/**
 * @description 清除所有localStorage
 * @return void
 */
export function localClear() {
  window.localStorage.clear()
}

export const handleCategoryModel = (data: any, key = 'modelid') => {
  if (!data) {
    return
  }

  return uniqBy(data, key)
}

// 输出通知，如果message为string是依次传入，如果是object则直接传入
export const notice = (message: string, type: any = 'success', title = '友情提醒', ...rest: any) => {
  ElNotification(typeof message === 'string' ? { title, message, type, ...rest } : message)
}
